package com.example.mall_image_server.configuration;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class GlobalCorsConfiguration {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1.设置允许任何域名
        corsConfiguration.addAllowedHeader("*"); // 2.设置允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3.设置允许任何方法(都可单一控制:get、post...)
        source.registerCorsConfiguration("/**", corsConfiguration); // 注册的跨域配置
        return new CorsFilter(source);
    }
}
